namespace SubhadraSolutions.Sharp.Logging.LogSubscribers.FileSystem
{
    using SubhadraSolutions.Sharp.Logging.Common;
    using SubhadraSolutions.Sharp.Logging.Contracts;
    using SubhadraSolutions.Sharp.Utils.Data;
    using System.Collections.Generic;

    public abstract class AbstractPerfCounterHtmlFileLogSubscriber : HtmlFileLogSubscriber
    {
        #region Fields

        private readonly ILogPublisher _logPublisher;

        private CounterCategoryInfoDataReader _dataReader;

        #endregion Fields

        #region Constructors

        protected AbstractPerfCounterHtmlFileLogSubscriber(ILogPublisher logPublisher)
        {
            _logPublisher = logPublisher;
        }

        #endregion Constructors

        #region Properties

        //protected override DataReaderDataTableAdapter getAdapter(IEnumerable<LogStoreItem> e)
        //{
        //    DataTable table = buildDataTable(e);
        //    if (table != null)
        //    {
        //        return new DataReaderDataTableAdapter(table);
        //    }
        //    return null;
        //}
        public string CounterCategoryName
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            _dataReader.Dispose();
        }

        protected override DataReaderDataTableAdapter getAdapter(IEnumerable<LogStoreItem> e)
        {
            _dataReader.SetContext(e);
            return new DataReaderDataTableAdapter(_dataReader, "Data");
        }

        protected override ILogPublisher getLogPublisher()
        {
            return _logPublisher;
        }

        protected override void initializeProtected()
        {
            _dataReader = new CounterCategoryInfoDataReader(CounterCategoryName);
            if (string.IsNullOrEmpty(Title))
            {
                Title = CounterCategoryName;
            }
            base.initializeProtected();
        }

        #endregion Methods

        #region Other

        //public DataTable buildDataTable(IEnumerable<LogStoreItem> e)
        //{
        //    DataTable table = null;
        //    try
        //    {
        //        foreach (var item in e)
        //        {
        //            CounterUpdateInfo cInfo = item.LogEntry as CounterUpdateInfo;
        //            if (cInfo == null)
        //            {
        //                continue;
        //            }
        //            CounterCategoryInfo info = cInfo.GetCategoryInfo(CounterCategoryName, false);
        //            if (info == null)
        //            {
        //                continue;
        //            }
        //            if (table == null)
        //            {
        //                table = new DataTable("data");
        //                table.Columns.Add("SNo", typeof(long));
        //                table.Columns.Add("TimeStamp", typeof(DateTime));
        //                table.Columns.Add("InstanceName", typeof(string));
        //                foreach (var counterName in info.ReferenceTypeCounterNames)
        //                {
        //                    table.Columns.Add(counterName.PutSpaceAtUpperCasing(), typeof(object));
        //                }
        //                foreach (var counterName in info.CounterNames)
        //                {
        //                    table.Columns.Add(counterName.PutSpaceAtUpperCasing(), typeof(double));
        //                }
        //            }
        //            foreach (var instance in info.Instances)
        //            {
        //                DataRow row = table.NewRow();
        //                info = item.LogEntry as CounterCategoryInfo;
        //                _logItemIndex++;
        //                row[0] = _logItemIndex;
        //                row[1] = item.TimeStamp;
        //                row[2] = instance.InstanceName;
        //                int i = 0;
        //                for (; i < instance.ReferenceTypeCounterValues.Length; i++)
        //                {
        //                    row[i + 3] = instance.ReferenceTypeCounterValues[i];
        //                }
        //                i += 3;
        //                for (int j = 0; j < instance.CounterValues.Length; j++)
        //                {
        //                    row[i + j] = instance.CounterValues[j];
        //                }
        //                table.Rows.Add(row);
        //            }
        //        }
        //        if (table != null)
        //        {
        //            table.AcceptChanges();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        System.Diagnostics.Debug.WriteLine(ex);
        //    }
        //    return table;
        //}

        #endregion Other
    }
}